//
//  HJMineViewController.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJMineViewController.h"
#import "HJNavigationBar.h"
#import "HJMineCell.h"
#import "HJSettingViewController.h"

#import "HJMineAccountController.h" // 我的账户
#import "HJPreferenceSettingController.h" // 偏好设置
#import "HJAskToBuyViewController.h" // 我的求购和购买
#import "HJSellViewController.h" // 我的出售
#import "HJGoodsAdressController.h"
#import "HJFeedbackController.h"// 意见反馈
//#import "EaseMessageViewController.h"// 客服

@interface HJMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)HJNavigationBar *navigationBar;
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong) NSArray *titleArr;
@property(nonatomic,strong) HJUserModel * userModel;
@end

@implementation HJMineViewController
-(NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"我的账户",@"偏好设置",@"我的求购和购买",@"我的出售",@"收货地址",@"联系客服",@"意见反馈"];
    }
    return _titleArr;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
//    [mNotificationCenter addObserver:self selector:@selector(outLogin) name:@"outLogin" object:nil];
    [HJUserManager isLogin];
//    if ([[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"]) {
         [self loadWork];
//    }
    
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}
//-(void)outLogin {
//    self.navigationBar.headImage.image = HJImage(@"atovor1");
//    self.navigationBar.userName.text = @"暂无登录";
//    self.navigationBar.userPhone.text = @"";
//    self.userModel.my_interest = @"";
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setBtn:) name:@"setBtn" object:nil];
    
    [self configNavigation];
    [self configTable];
}

-(void)configNavigation {
    self.navigationBar = [[HJNavigationBar alloc]init];
    self.navigationBar.frame = CGRectMake(0, 0, mScreenWidth, 104);
    [self.view addSubview:self.navigationBar];
}

// 导航跳转通知
-(void)setBtn:(NSNotification *)not {
    if ([HJUserManager isLoginView]) {
        return;
    }
    UIStoryboard * sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    HJSettingViewController * vc = [sb instantiateViewControllerWithIdentifier:@"setting"];
    vc.userModel =self.userModel;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.navigationBar.frame = CGRectMake(0, 0, mScreenWidth, 104);
}

-(void)configTable {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 104, mScreenWidth, mScreenHeight -114-TabBar_H) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.tableFooterView = [UIView new];
//    [self.tableView registerCellByNibName:@"HJMineCell"];
    [self.view addSubview:self.tableView];
}

-(void)loadWork {
    [TZHttpTool post:InfoAPI params:nil success:^(NSDictionary *result) {
        self.userModel = [[HJUserModel alloc]init];
        self.userModel = [HJUserModel mj_objectWithKeyValues:result[@"data"]];
        self.navigationBar.model = self.userModel;
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [self showErrorHUDWithError:msg];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJMineCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"HJMineCell" owner:nil options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (self.titleArr) {
        cell.titleLabel.text = _titleArr[indexPath.row];
    }
    if (indexPath.row ==0) {
        cell.content.hidden = NO;
        cell.sellLabel.hidden = YES;
        if (!self.userModel.money ||!self.userModel.forzen_money) {
            cell.content.text = [NSString stringWithFormat:@"可提现余额￥%@ 冻结余额￥%@",@"0.00",@"0.00"];
        }else {
           cell.content.text = [NSString stringWithFormat:@"可提现余额￥%@ 冻结余额￥%@",self.userModel.money,self.userModel.forzen_money];
        }
    }else if (indexPath.row == 3) {
        cell.content.hidden = YES;
        cell.sellLabel.hidden = NO;
        if ([[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"]) {
            if ([self.userModel.my_sell isEqual:@0]) {
                cell.sellLabel.hidden = YES;
            }else {
                cell.sellLabel.text = @"您有一件商品售出";
            }
        }else {
            cell.sellLabel.hidden = YES;
        }
    }else if (indexPath.row == 2) {
        if ([[mUserDefaults objectForKey:@"isLogin"] isEqualToString:@"1"]) {
            if ([self.userModel.my_buy isEqual:@0]) {
                cell.tworowLabel.hidden = YES;
            }else {
                cell.tworowLabel.hidden = NO;
            }
        }
        cell.content.hidden = YES;
        cell.sellLabel.hidden = YES;
    }else if (indexPath.row == 1) {
        cell.tworowLabel.text = self.userModel.my_interest;
        cell.tworowLabel.hidden = NO;
        cell.sellLabel.hidden = YES;
        cell.content.hidden = YES;
    }else {
        cell.content.hidden = YES;
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([HJUserManager isLoginView]) {
        return;
    }
    if (indexPath.row == 0) {
        HJMineAccountController *mineAccountVC =[[HJMineAccountController alloc]init];
        mineAccountVC.navTitle = @"我的账户";
        [self.navigationController pushViewController:mineAccountVC animated:YES];
    }else if (indexPath.row ==1) {
        HJPreferenceSettingController * preferrnceVC = [[HJPreferenceSettingController alloc]init];
        preferrnceVC.navTitle = @"偏好设置";
        [self.navigationController pushViewController:preferrnceVC animated:YES];
    }else if (indexPath.row ==2) {
        HJAskToBuyViewController * asktobuyVC = [[HJAskToBuyViewController alloc]init];
        asktobuyVC.navTitle = @"我的求购和购买";
        asktobuyVC.num_tag = @"10";
        [self.navigationController pushViewController:asktobuyVC animated:YES];
    }else if (indexPath.row ==3) {
        HJSellViewController *sellVC = [[HJSellViewController  alloc]init];
        sellVC.navTitle = @"我的出售";
        [self.navigationController pushViewController:sellVC animated:YES];
    }else if (indexPath.row ==4) {
        HJGoodsAdressController * goodsAVC = [[HJGoodsAdressController alloc]init];
        goodsAVC.navTitle = @"收货地址";
        goodsAVC.enditBool = YES;
        [self.navigationController pushViewController:goodsAVC animated:YES];
    }else if (indexPath.row ==5) {
        HDMessageViewController *chatVC = [[HDMessageViewController alloc] initWithConversationChatter:@"13532523423"];
        chatVC.titleName = @"联系客服";
        [self.navigationController pushViewController:chatVC animated:YES];
    }else if (indexPath.row ==6) {
        HJFeedbackController * feedBackVC= [[HJFeedbackController alloc]init];
        feedBackVC.navTitle = @"意见反馈";
        [self.navigationController pushViewController:feedBackVC animated:YES];
    }
}

@end
